package net.hymnal.church;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import android.database.CharArrayBuffer;
import android.util.Log;

public class Settings {
	private final int CERRENT_SETTING_FILE_SIZE = 5;
	
	
	final byte DEF_TEXT_SIZE = 24;
	final int  DEF_THEME_ID = android.R.style.Theme_Light;
	
	public byte mTextSize;
	public int themeID; 
	boolean themeColorWhite = false;
	public Settings() {		
		mTextSize = (byte)DEF_TEXT_SIZE;		
		themeID = DEF_THEME_ID;		
	}
		
	public static String mFileName = new String("setting.dat");
	
	void loadSetting(File dir) throws IOException {	
		
		File file = null;
		FileReader fileReader = null;		
		try{
    	    file = new File(dir, mFileName);	
   	 	    fileReader = new FileReader(file);
		} catch (NullPointerException npe) {			
            Log.e("NullPointerException: Wrong parametr.", npe.getMessage() + " " + mFileName);
		} catch (FileNotFoundException fne) {
			Log.e("FileNotFoundException: Wrong parametr.", fne.getMessage() + " " + mFileName);
		} finally {
			if ((file != null) && (fileReader != null)) {
				char[] fileChar = new char[CERRENT_SETTING_FILE_SIZE];
				int readBytes = -1;
				try {
					readBytes = fileReader.read(fileChar);
				} catch (IOException ioe) {
					Log.e("File read error:", ioe.getMessage());
					return;
				}
				if (readBytes < 0) {
					throw new IOException(
							"The data can not be read from file: " + mFileName);
				}
				if (readBytes == CERRENT_SETTING_FILE_SIZE) {
					
					mTextSize = (byte) fileChar[0];
					//byte[] charByte = new byte[4];
					//for(int i = 0;i < 4; i++){
					//	charByte[i] = (byte)fileChar[i + 1];
					//}
					themeID = getInt(fileReader, fileChar, 1);
					//themeID = (int)(charByte[3] & 0x000000ff) +
					//		  (int)(((charByte[2] & 0x000000ff)) << 8) + 
					//		  (int)(((charByte[1] & 0x000000ff)) << 16) +
					//		  (int)((charByte[0] & 0x000000ff) << 24);
				}
				fileReader.close();				
			}			
		}   	 	
	}

	void saveSetting(File dir) throws IOException {
		int readResult = 0;
		File file = null;
		FileWriter fileWrriter = null; 
		try{
    	    file = new File(dir, mFileName);	
   	 	    fileWrriter  = new FileWriter(file);
		} catch (NullPointerException npe) {			
            Log.e("NullPointerException: Wrong parametr.", npe.getMessage() + " " + mFileName);
		} catch (FileNotFoundException fne) {
			Log.e("FileNotFoundException: Wrong parametr.", fne.getMessage() + " " + mFileName);
		} finally {
			if (fileWrriter != null) {
				fileWrriter.write((char) mTextSize);
				mTextSize = (byte) readResult;				
				saveInt(fileWrriter, themeID);								
				fileWrriter.close();
			}
		}
	}
	void switchTheme(){
		if(themeID == android.R.style.Theme_Light) {
			themeID = android.R.style.Theme_Black;
		} else {
			themeID = android.R.style.Theme_Light;
		}		
	}

	public int getCurrentTheme() {
		return themeID;
	}
	
	public boolean isBlack() {
		if (themeID == android.R.style.Theme_Light) {
			return false;
		}
		return true;
	}
	
	static void saveInt(FileWriter fw, int num) throws IOException{
		byte[] aByte = ByteBuffer.allocate(4).putInt(num).array();
		char[] aChar = new char[4];
		for(int i = 0; i < aChar.length; i++){
			aChar[i] = (char)aByte[i]; 
		}
		fw.write(aChar);		
	}
	
	/*
	 * Bytes size should be (4 + offset).
	 */
	static int getInt(FileReader fr,char[] bytes, int offset) throws IOException{
		int num = (int)(bytes[3 + offset] & 0x000000ff) +
				  (int)(((bytes[2 + offset] & 0x000000ff)) << 8) + 
				  (int)(((bytes[1 + offset] & 0x000000ff)) << 16) +
				  (int)((bytes[0 + offset] & 0x000000ff) << 24);
		return num;
	}
	
}
